package com.golang.activity.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.golang.activity.dto.request.ActivityBasicDto;
import com.golang.activity.dto.request.ActivitySettingDto;
import com.golang.activity.dto.response.*;
import com.golang.activity.repository.ActivityRepository;
import com.golang.activity.service.ActivityService;
import com.ruoyi.system.domain.Activity;
import com.ruoyi.system.domain.ActivityProcess;
import com.ruoyi.system.domain.ActivityTag;
import com.ruoyi.system.domain.ActivityTicket;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
public class ActivityServiceImpl implements ActivityService {

    @Resource
    ActivityRepository activityRepository;

    @Override
    public String createBasic(ActivityBasicDto basic) {
        Activity activity = activityBasic(basic).apply(basic);
        boolean insertFlag = activityRepository.insertActivity(activity) > 0;
        if (insertFlag) {
            //保存活动流程
            basic.getProcessList().forEach(o -> {
                ActivityProcess process = activityProcessDto2ActivityProcess(o, activity.getActivityId()).apply(o);
                activityRepository.insertActivityProcess(process);
            });

            //保存标签
            basic.getTagList().forEach(o -> {
                ActivityTag tag = activityTagDto2ActivityTag(o, activity.getActivityId()).apply(o);
                activityRepository.insertActivityTag(tag);
            });
            return activity.getActivityId();
        }
        return null;
    }

    @Override
    public Boolean updateBasic(ActivityBasicDto basic) {
        return null;
    }

    @Override
    public ActivityBasicRespDto getBasic(String activityId) {
        Activity entity = activityRepository.queryActivity(activityId);

        ActivityBasicRespDto activityBasicRespDto = ActivityBasic2Dto(entity).apply(entity);
        List<ActivityProcessRespDto> processDtoList = CollUtil.list(false);
        List<ActivityProcess> processList = activityRepository.queryActivityProcess(activityId);
        //流程列表
        processList.forEach(process -> {
            ActivityProcessRespDto processDto = ActivityProcess2Dto(process).apply(process);
            processDtoList.add(processDto);
        });

        activityBasicRespDto.setProcessList(processDtoList);

        List<ActivityTagRespDto> tagDtoList = CollUtil.list(false);
        List<ActivityTag> tagList = activityRepository.queryActivityTag(activityId);
        //标签列表
        tagList.forEach(tag -> {
            ActivityTagRespDto tagDto = ActivityTag2Dto(tag).apply(tag);
            tagDtoList.add(tagDto);
        });

        return activityBasicRespDto.setTagList(tagDtoList);

    }

    @Override
    public Boolean createSetting(ActivitySettingDto setting) {

        Activity activity = activityRepository.queryActivity(setting.getActivityId());

        activity = activitySetting(setting, activity).apply(setting);

        boolean updateFlag = activityRepository.updateActivity(activity) > 0;

        if (updateFlag) {
            //保存票种配置信息
            Activity finalActivity = activity;
            setting.getTicketList().forEach(o -> {
                ActivityTicket ticket = activityTicketDto2ActivityTicket(o, finalActivity.getActivityId(), setting.getRefundDeadline()).apply(o);
                activityRepository.insertActivityTicket(ticket);
            });
        }

        return updateFlag;
    }

    @Override
    public ActivitySettingRespDto getSetting(String activityId) {
        Activity entity = activityRepository.queryActivity(activityId);

        ActivitySettingRespDto activitySettingRespDto = ActivitySetting2Dto(entity).apply(entity);
        List<ActivityTicketRespDto> ticketDtoList = CollUtil.list(false);
        List<ActivityTicket> ticketList = activityRepository.queryActivityTicket(activityId);
        //流程列表
        ticketList.forEach(ticket -> {
            ActivityTicketRespDto ticketDto = ActivityProcess2Dto(ticket).apply(ticket);
            ticketDtoList.add(ticketDto);
        });

        return activitySettingRespDto.setTicketList(ticketDtoList);
    }

    @Override
    @Transactional
    public Boolean copyHistory(String activityId) {

        Activity activitySource = activityRepository.queryActivity(activityId);
        Activity activityTarget = new Activity();
        BeanUtils.copyProperties(activitySource, activityTarget);
        activityTarget.setId(null);


        return true;


    }
}
